﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgriMarketWebProj.AgriMarketService;
using AgriMarketWebProj.Models;
namespace AgriMarketWebProj.Controllers
{
    public class UtilityController : Controller
    {
        //
        // GET: /Utility/
        public ActionResult CheckLogIn_Require()
        {
            AgriMarketWebService service = new AgriMarketWebService();
            UserAccount userAccount = null;
            if (Session["CurrentUserAccount"] != null)
            {
                userAccount = service.GetUserAccountByUserName(Session["CurrentUserAccount"].ToString());
                ViewBag.CurrentUserAccount = userAccount;
                if (userAccount.TypeOfUser.TypeId != AdminCode.AdminTypeCode)
                    return Content("Bạn không có quyền truy cập!");
                return null;
            }
            else
            {
                TempData["ReturnUrl"] = Request.Url.PathAndQuery;
                return RedirectToAction("LogIn", "UserAccount");
            }
        }
        public void CheckLogIn_NoRequire()
        {
            AgriMarketWebService service = new AgriMarketWebService();
            UserAccount userAccount = null;
            if (Session["CurrentUserAccount"] != null)
            {
                userAccount = service.GetUserAccountByUserName(Session["CurrentUserAccount"].ToString());
                ViewBag.CurrentUserAccount = userAccount;
            }
        }
        public ActionResult Index()
        {
            AgriMarketWebService service = new AgriMarketWebService();
            ActionResult actionResult = CheckLogIn_Require();
            if (actionResult != null)
                return actionResult;
            //List<Auction> unSentAuctions = service.GetUnSentAuctions().ToList() ;
            return View(service.GetUnSentAuctions());
        }
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            string[] auctionIdsStr = collection["AuctionId[]"].Split(',');
            List<int> auctionIds = new List<int>();
            foreach (var item in auctionIdsStr)
            {
                auctionIds.Add(int.Parse(item));
            }
            AgriMarketWebService service = new AgriMarketWebService();
            ActionResult actionResult = CheckLogIn_Require();
            if (actionResult != null)
                return actionResult;
            var states = service.SendEmailtoSomeWinners(auctionIds.ToArray());
            var unSentAuctions = service.GetUnSentAuctions();
            return View(unSentAuctions);
        }
    }
}
